package com.code.leetcode.offer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author codeDorado
 * @version 1.0
 * @date 2020/8/1 10:45
 */
public class The29 {
    public int[] spiralOrder(int[][] matrix) {
        if (matrix == null || matrix.length < 1 || matrix[0].length < 1) return new int[]{};
        int m = matrix.length;
        int n = matrix[0].length;
        int[] rtn = new int[m * n];
        int index = 0;
        int top = 0, botton = m - 1, left = 0, right = n - 1;
        while (top <= botton && left <= right) {
            for (int i = left; i <= right; i++) {
                rtn[index++] = matrix[top][i];
            }
            for (int i = top + 1; i <= botton; i++) {
                rtn[index++] = matrix[i][right];
            }
            if (top < botton && left < right) {
                for (int i = right - 1; i >= left; i--) {
                    rtn[index++] = matrix[botton][i];
                }
                for (int i = botton - 1; i > top; i--) {
                    rtn[index++] = matrix[i][left];
                }
            }
            top++;
            botton--;
            left++;
            right--;
        }
        return rtn;
    }
}
